'use strict'

let {paramcheck} = require('titbit-toolkit')

let pck = new paramcheck({
    key: 'body',
    data: {
        id: {
            must: true
        },

        val: {
            must: true
        }
    }
})

class user {
    constructor() {

    }
    // 检验用户传入的验证码是否正确
    async post(c) {
        //let body = JSON.parse(c.body.toString());//用户输入答案数据
        // console.log(body)

        let img = await c.service.pqorm.model('img')
                                .where('id=? OR oid=?', [c.body.id, c.body.id])
                                .get()

        if (!img) {
            return c.status(400).send('用户输入超时');
        }

        if (img.answer !== c.body.val) {
            return c.status(400).send('验证码有误');
        }

        c.send('验证成功');
    
    /* 
        let imgarr = await c.service.pqorm.model('img').select('*');
        let id = await c.service.pqorm.model('img').select('id');
        console.log(id);

        // let answer = await c.service.pqorm('img').select('answer');
        let flag = false;
        for (let i = 0; i < imgarr.length; i++) {
            //body里面的val是用户输入的值
            if (imgarr[i].id == body.id && imgarr[i].answer == body.val) {
                flag = true;
            }
        }
        if (flag == true) {
            c.send('验证成功');
            // console.log('successful');
        } else {
            c.send('验证失败');
            // console.log('fail');
        } */
    }

    __mid() {
        return [
            {
                middleware: pck,
                path: [
                    'post'
                ]
            }
        ]
    }
}

module.exports = user;